package walk

import (
	"gitee.com/snxamdf/wui/logs"
	"html/template"
	"net/http"
)

var indexHtmlTemplate = `<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <script type="text/javascript">var ROOT_PATH = "{{.RootPath}}/lib/"</script>
    <script type="text/javascript" src="/static/lib/boot.js"></script>
    <script type="text/javascript">
        $(function () {
            new App({
                wrapper: {
                    el: "#frameId",
                    url: "{{.IndexUrl}}"
                },
        		created: function () {
					//初始化完必回调
				}
            });
        });
    </script>
</head>
<body id="frameId" style="-ms-overflow-style:none;">

</body>
</html>
`

func indexServer(data data) {
	logs.Info("app index start...")
	http.HandleFunc("/"+data.randomIndex, func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Pragma", "no-cache")
		w.Header().Set("Cache-Control", "no-cache, no-store, max-age=0")
		w.Header().Set("Expires", "1L")
		w.Header().Set("Content-Type", "text/html")
		w.WriteHeader(200)
		tmpl, err := template.New("indexHtmlTemplate").Parse(indexHtmlTemplate)
		if err != nil {
			logs.Info("app index frame Error ->", err)
		} else {
			if err = tmpl.Execute(w, data); err != nil {
				logs.Info("app index template Error ->", err)
			}
		}
	})
	logs.Info("app index start ok")
}

type data struct {
	WebGUI
	randomIndex string
	RootPath    string
}
